package com.ryan.test.viewpage;

import java.util.Iterator;
import java.util.concurrent.Delayed;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.animation.Interpolator;
import android.widget.Scroller;
import android.widget.TextView;

public class MenuView extends ViewGroup {

	public MenuView(Context context) {
		super(context);
		initView();
	}

	public MenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView() {
		mScroller = new Scroller(getContext(), sInterpolator);

		LayoutParams leftParams = new LayoutParams(250,
				LayoutParams.FILL_PARENT);
		TextView leftChild = new TextView(getContext());
		leftChild.setText("1234567890");
		leftChild.setBackgroundColor(Color.GRAY);
		addView(leftChild, 0, leftParams);

		LayoutParams rightParams = new LayoutParams(250,
				LayoutParams.FILL_PARENT);
		TextView rightChild = new TextView(getContext());
		rightChild.setText("abcdefghijklmn");
		rightChild.setBackgroundColor(Color.RED);
		addView(rightChild, 1, rightParams);

		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		TextView child = new TextView(getContext());
		child.setText("qwertyuiop");
		child.setBackgroundColor(Color.WHITE);
		addView(child, 1, params);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		System.out.println("--------onLayout");
		int left = 0;
		int top = 0;
		View view = getChildAt(1);
		view.layout(left, top, view.getMeasuredWidth(),
				view.getMeasuredHeight());

		View leftView = getChildAt(0);
		leftView.layout(left - leftView.getMeasuredWidth(), top, left,
				leftView.getMeasuredHeight());

		View rightView = getChildAt(2);
		rightView.layout(left + view.getMeasuredWidth(), top,
				left + view.getMeasuredWidth() + rightView.getMeasuredWidth(),
				rightView.getMeasuredHeight());

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		System.out.println("--------onMeasure");
		setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
				getDefaultSize(0, heightMeasureSpec));

		int childWidthSize = getMeasuredWidth() - getPaddingLeft()
				- getPaddingRight();
		int childHeightSize = getMeasuredHeight() - getPaddingTop()
				- getPaddingBottom();

		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);
			final int wid = measureSpec(view.getLayoutParams().width,
					childWidthSize);
			final int hei = measureSpec(view.getLayoutParams().height,
					childHeightSize);
			view.measure(wid, hei);
		}
	}

	private int measureSpec(int layout, int def) {
		int mode;
		if (layout == LayoutParams.FILL_PARENT) {
			mode = MeasureSpec.EXACTLY;
		} else if (layout == LayoutParams.WRAP_CONTENT) {
			mode = MeasureSpec.AT_MOST;
		} else {
			mode = MeasureSpec.EXACTLY;
			def = layout;
		}
		return MeasureSpec.makeMeasureSpec(def, mode);
	}

	private float mLastTouchX;
	private float mInitTouchX;
	private VelocityTracker mVelocityTracker;
	private float mMaximumVelocity = 4000F;
	private Scroller mScroller;
	private int mPos;
	private static final Interpolator sInterpolator = new Interpolator() {
		public float getInterpolation(float t) {
			t -= 1.0f;
			return t * t * t * t * t + 1.0f;
			// return t + 1.0f;
		}
	};

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		final int action = event.getAction();

		System.out.println(action + "----------------" + getScrollX() + " "
				+ event.getX());
		switch (action & MotionEventCompat.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mLastTouchX = event.getX();
			mInitTouchX = event.getX();
			startDrag();
			break;
		case MotionEvent.ACTION_MOVE:
			mVelocityTracker.addMovement(event);
			float deltaX = event.getX() - mLastTouchX;
			mLastTouchX = event.getX();
			float scroll = getScrollX() + (-1) * deltaX;
			scrollTo((int) scroll, getScrollY());
			break;
		case MotionEvent.ACTION_UP:

			mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
			float velocity = mVelocityTracker.getXVelocity();
			float totalDeltaX = event.getX() - mInitTouchX;
			if (Math.abs(totalDeltaX) > 50 && velocity > 50) {
				mScroller.startScroll(getScrollX(), getScrollY(), 200, 0, 100);
			} else {
				mScroller.startScroll(getScrollX(), getScrollY(), -200, 0, 100);
			}
			invalidate();
			endDrag();
		}

		return true;
	}

	private void startDrag() {
		if (mVelocityTracker == null)
			mVelocityTracker = VelocityTracker.obtain();
	}

	private void endDrag() {
		if (mVelocityTracker != null) {
			mVelocityTracker.recycle();
			mVelocityTracker = null;
		}
	}

	@Override
	public void computeScroll() {
		if (!mScroller.isFinished()) {
			if (mScroller.computeScrollOffset()) {
				int oldX = getScrollX();
				int oldY = getScrollY();
				int x = mScroller.getCurrX();
				int y = mScroller.getCurrY();

				if (oldX != x || oldY != y) {
					scrollTo(x, y);
				}

				// Keep on drawing until the animation has finished.
				invalidate();
				return;
			}
		}

	}
}
